package com.rbcs.base.currency.webapp;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.rbcs.base.currency.service.CurrencyManager;
import com.rbcs.base.currency.service.ExchangeRateManager;
import com.rbcs.base.model.Currency;
import com.rbcs.base.model.ExchangeRate;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.common.modules.security.user.model.User;
import com.systop.core.dao.support.Page;
import com.systop.core.util.PageUtil;
import com.systop.core.webapp.struts2.action.JsonCrudAction;

/**
 * 外币汇率管理Action
 * 
 * @author ShangHua
 */
@SuppressWarnings({ "serial", "rawtypes", "unchecked" })
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class ExchangeRateAction extends
		JsonCrudAction<ExchangeRate, ExchangeRateManager> {

	/**
	 * 币种管理Manager
	 */
	@Autowired
	private CurrencyManager currencyManager;

	/**
	 * Json数据
	 */
	private Map<String, String> jsonResult;

	/**
	 * 查看外币币种信息
	 */
	@Override
	public String index() {
		Page page = PageUtil.getPage(getPageNo(), getPageSize());
		StringBuffer hql = new StringBuffer("from ExchangeRate er where 1 = 1");
		List args = new ArrayList();
		if (getModel()!= null && getModel().getCurrency() != null 
				&& getModel().getCurrency().getId() != null) {
			hql.append(" and er.currency.id = ?");
			args.add(getModel().getCurrency().getId());
		}
		if (StringUtils.isNotBlank(getModel().getYear())) {
			hql.append(" and er.year = ?");
			args.add(getModel().getYear());
		}
		page = getManager().pageQuery(page, hql.toString(), args.toArray());
		restorePageData(page);
		return INDEX;
	}
	
	/**
	 * 生成币种汇率数据【AJAX调用】
	 * 
	 * @return
	 */
	public String generateData() {
		jsonResult = Collections.synchronizedMap(new HashMap<String, String>());
		String currencyId = getRequest().getParameter("currencyId");
		String year = getRequest().getParameter("year");
		if (getLoginUser() == null) {
			jsonResult.put("result", "error");
			return "jsonResult";
		}
		try {
			if (StringUtils.isNotBlank(currencyId)) {
				ExchangeRate rate = getManager().getExchangeRate(Integer.valueOf(currencyId), year);
				if (rate != null) {
					getManager().remove(rate);
				}
				ExchangeRate exchangeRate = new ExchangeRate();
				Currency currency = currencyManager.get(Integer.valueOf(currencyId));
				exchangeRate.setCurrency(currency);
				exchangeRate.setYear(year);
				getManager().save(exchangeRate);
				jsonResult.put("result", "success");
			} else {
				List<Currency> currencyList = currencyManager.getCurrencyList();
				for (Currency currency : currencyList) {
					ExchangeRate rate = getManager().getExchangeRate(currency.getId(), year);
					if (rate != null) {
						getManager().remove(rate);
					}
					ExchangeRate exchangeRate = new ExchangeRate();
					exchangeRate.setCurrency(currency);
					exchangeRate.setYear(year);
					getManager().save(exchangeRate);
				}
				jsonResult.put("result", "success");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "jsonResult";
	}

	/**
	 * 异步调用，修改各个币种汇率信息
	 * 
	 * @return
	 */
	public String updateExchangeRate() {
		jsonResult = Collections.synchronizedMap(new HashMap<String, String>());
		String id = getRequest().getParameter("id");
		String rate = getRequest().getParameter("rate");
		String month = getRequest().getParameter("month");
		//System.out.println("@@@@@@@@@@:" + id + "/" + rate + "/" + month);
		if (StringUtils.isNotBlank(id)) {
			ExchangeRate exchangeRate = getManager().get(Integer.valueOf(id));
			if (exchangeRate != null) {
				if(month.equals("1")){
					exchangeRate.setJanuaryRate(rateFormat(rate));
				}else if (month.equals("2")) {
					exchangeRate.setFebruaryRate(rateFormat(rate));
				}else if (month.equals("3")) {
					exchangeRate.setMarchRate(rateFormat(rate));
				}else if (month.equals("4")) {
					exchangeRate.setAprilRate(rateFormat(rate));
				}else if (month.equals("5")) {
					exchangeRate.setMayRate(rateFormat(rate));
				}else if (month.equals("6")) {
					exchangeRate.setJuneRate(rateFormat(rate));
				}else if (month.equals("7")) {
					exchangeRate.setJulyRate(rateFormat(rate));
				}else if (month.equals("8")) {
					exchangeRate.setAugustRate(rateFormat(rate));
				}else if (month.equals("9")) {
					exchangeRate.setSeptemberRate(rateFormat(rate));
				}else if (month.equals("10")) {
					exchangeRate.setOctoberRate(rateFormat(rate));
				}else if (month.equals("11")) {
					exchangeRate.setNovemberRate(rateFormat(rate));
				}else if (month.equals("12")) {
					exchangeRate.setDecemberRate(rateFormat(rate));
				}
				getManager().update(exchangeRate);
			}
		}
		
		jsonResult.put("result", "success");
		return "jsonResult";
	}
	
	/**
	 * 格式化利率
	 * @param rate
	 * @return
	 */
	private float rateFormat(String rate) {
		float rates = 0;
		if (StringUtils.isNotBlank(rate)) {
			return Float.valueOf(rate);
		}else {
			return rates;
		}
	}
	
	/**
	 * 得到登录用户User
	 */

	private User getLoginUser() {
		User user = UserUtil.getPrincipal(getRequest());
		return user;
	}

	/**
	 * 返回所有的币种名称集合
	 * 
	 * @return list
	 */
	public List<Map> getCurrencyList() {
		List list = Collections.EMPTY_LIST;
		list = currencyManager.getCurrencyInfo();
		return list;
	}

	public Map<String, String> getJsonResult() {
		return jsonResult;
	}

	public void setJsonResult(Map<String, String> jsonResult) {
		this.jsonResult = jsonResult;
	}
}
